#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :文件处理.py
# @Time      :2024/3/6 
# @Author    :CL
# @email     :1037654919@qq.com
import  os
import json
import shutil
# pinterest 打开与ff同名的json文件获取id，重命名，移动文件到新文件夹
def test_20240307():
    path = f'/media/chenglei3/77D014CED257D1411/images/pinterest待处理/'
    files = os.listdir(path)
    print(files)
    for f in  files:
        filename = path + f
        os.makedirs(f'/media/chenglei3/77D014CED257D1411/images/pinterest/{f}', exist_ok=True)
        print(f'正在处理{filename}')
        filess = os.listdir(filename)
        for ff in filess:
            if ff.endswith('.jpg'):
                # 打开与ff同名的json文件获取id，重命名，移动文件到新文件夹
                try:
                    with open(filename + '/' + ff.split('.')[0]+'.json', 'r') as f_:
                        data = json.load(f_)
                        # print(dict(data))
                        id = dict(data)['_id']
                    # 重命名并移动文件
                    newpath = f'/media/chenglei3/77D014CED257D1411/images/pinterest/'
                    original_path_img =  path + f + '/' + ff
                    target_path_img = newpath + f + '/' + id + '.jpg'
                    original_path_json =  path + f + '/' +  ff.split('.')[0]+'.json'
                    target_path_json = newpath + f + '/' + id + '.json'

                    shutil.move(original_path_img, target_path_img)
                    shutil.move(original_path_json, target_path_json)
                except Exception as  e:
                    print(e)
            elif  ff.endswith('.gif'):
                # 删除ff和与ff同名的json文件
                os.remove(filename + '/' + ff)
                os.remove(filename + '/' + ff.split('.')[0]+'.json')

if  __name__ == '__main__':
    print()
